package com.anXin.runerrands.service.impl;

import cn.hutool.json.JSONUtil;
import com.anXin.apis.user.IUserClient;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.service.dtos.RunerrandsReleaseDTO;
import com.anXin.model.service.pojos.RunerrandsResort;
import com.anXin.model.user.dtos.UserInfoDto;
import com.anXin.runerrands.mapper.RunerrandsResortMapper;
import com.anXin.runerrands.service.RunerrandsResortService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 跑腿服务
 * @author 鹏
 */
@Slf4j
@Service
public class RunerrandsResortServiceImpl extends ServiceImpl<RunerrandsResortMapper, RunerrandsResort> implements RunerrandsResortService {

    @Resource
    private IUserClient iUserClient;

    /**
     * 发布求助帖
     * @param runerrandsReleaseDTO 前端发送来的数据
     */
    @Override
    public ResponseResult add(RunerrandsReleaseDTO runerrandsReleaseDTO) {
        log.info("进入Impl中: {}",runerrandsReleaseDTO);
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        if (user.getId().equals(runerrandsReleaseDTO.getAuthorId())) {
            RunerrandsResort runerrandsResort = new RunerrandsResort();
            BeanUtils.copyProperties(runerrandsReleaseDTO, runerrandsResort);
            runerrandsResort.setCreatedTime(new Date());
            runerrandsResort.setState(1);
            runerrandsResort.setAuthorAvatar(user.getImage());
            runerrandsResort.setAuthorName(user.getName());
            this.save(runerrandsResort);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), "帖子发送成功!");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(), "无权限操作");
        }
    }

    /**
     * 删除求助帖
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Long id) {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        if (this.getById(id).getAuthorId().equals(user.getId())){
            boolean row = this.removeById(id);
            if (!row){
                return ResponseResult.errorResult(AppHttpCodeEnum.OPERATE_BUSY_ERROR.getCode(),"操作失败,系统繁忙,请稍后重试");
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(),"无权限操作");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"帖子删除成功!");
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @Override
    public ResponseResult pass(Long id){
        RunerrandsResort runerrandsResort = this.getById(id);
        runerrandsResort.setState(0);
        this.updateById(runerrandsResort);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"帖子已允许发布!");
    }

    /**
     * 审核不通过
     * @param id
     * @return
     */
    @Override
    public ResponseResult fail(Long id){
        RunerrandsResort runerrandsResort = this.getById(id);
        runerrandsResort.setState(2);
        this.updateById(runerrandsResort);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"帖子已经退回!");
    }

    /**
     * 展示所有正常发布的求助帖
     * @return
     */
    @Override
    public ResponseResult listNormal() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<RunerrandsResort> runerrandsResorts = query().eq("state",0).eq("author_id",user.getId()).list();
        return ResponseResult.okResult(runerrandsResorts);
    }

    /**
     * 展示所有待审核的求助帖
     * @return
     */
    @Override
    public ResponseResult listReview() {
        log.info("收到前端的请求");
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<RunerrandsResort> runerrandsResorts = query().eq("state",1).eq("author_id",user.getId()).list();
        return ResponseResult.okResult(runerrandsResorts);
    }

    /**
     * 展示所有已经撤下或违禁退回的求助帖
     * @return
     */
    @Override
    public ResponseResult listError() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<RunerrandsResort> runerrandsResorts = query().eq("state",2).eq("author_id",user.getId()).list();
        return ResponseResult.okResult(runerrandsResorts);
    }

    /**
     * 展示所有正在订单进行中的求助帖
     * @return
     */
    @Override
    public ResponseResult listOrder() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        log.info("获取到用户信息: {}",user);
        List<RunerrandsResort> runerrandsResorts = query().eq("state",3).and(i -> i.eq("author_id",user.getId())).list();
        return ResponseResult.okResult(runerrandsResorts);
    }

}
